package com.tjbank.cssys.service.impl;

import com.tjbank.cssys.api.IamLogService;
import com.tjbank.cssys.dto.IamLogDTO;
import com.tjbank.cssys.framework.base.util.GeneratIDUtil;
import com.tjbank.cssys.mybatis.IamLogDao;
import com.tjbank.cssys.mybatis.entity.IamLog;
import com.tjbank.cssys.mybatis.entity.IamLogKey;
import com.tjbank.cssys.mybatis.sqlbuilder.IamLogSqlBuilder;
import com.tjbank.cssys.service.convert.IamLogConvert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class IamLogServiceImpl implements IamLogService {

    @Autowired
    private IamLogDao iamLogDao;

    @Override
    public Integer addLog(IamLogDTO logDTO) {

        IamLog entity = IamLogConvert.INSTANCE.dtoToEntity(logDTO);
        entity.setId(GeneratIDUtil.getUUId());
        entity.setLogTime(new Date());

        return iamLogDao.insert(entity);
    }

    @Override
    public Integer deleteLog(String logId) {
        return iamLogDao.deleteByPrimaryKey(new IamLogKey(logId));
    }

    @Override
    public List<IamLogDTO> getLog(String logType, String logUser) {
        IamLogSqlBuilder iamLogSqlBuilder = new IamLogSqlBuilder();
        iamLogSqlBuilder.andLogTypeEqualTo(logType).andLogUserEqualTo(logUser);

        return IamLogConvert.INSTANCE.entitiesToDtoList(iamLogDao.selectListBySqlBuilder(iamLogSqlBuilder));
    }
}
